package com.ms.es.support;

import com.ms.es.connection.EsDataSource;
import com.ms.es.connection.EsRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

@Slf4j
public class ServiceProxy<T> implements InvocationHandler {

    private final T target;

    private final EsDataSource dataSource;

    public ServiceProxy(T target, EsDataSource dataSource) {
        this.target = target;
        this.dataSource = dataSource;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        try {
            EsRequest esRequest = new EsRequest(method, args, (Class<?>) target, dataSource);
            return esRequest.invoke();
        } catch (Exception e) {
            log.error("es execute error:{}", e.getMessage());
            throw e;
        }
    }

}
